package com.iwop.moto.controller;

import com.alibaba.fastjson.JSONArray;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.IDataService;
import com.iwop.moto.utils.stringUtil.SequenceUtil;
import com.iwop.moto.utils.stringUtil.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 数据字典
 * @author huyuangui on 2016/7/25.
 */
@Controller
public class DataController extends  BaseController {
    @Autowired
    private IDataService dataService;
    /***
     * 根据类型获取数据字典列表
     * @author huyuangui on 2016/7/25.
     * 参数 parentname 上级名称
     * @return
     */
    @RequestMapping("/api/findDataByParent")
    @ResponseBody
    public ResultModel findDataByParent(){
        ResultModel resultModel=new ResultModel();
        SuperRecordModel superRecordModel=this.getSRModel();
        if(StringUtils.isEmpty(superRecordModel.getString("parentname"))){
            resultModel.setMem(ErrCode.ERROR_PARAMETER);
            resultModel.setCode(0);
            return resultModel;
        }
        List<SuperRecordModel> list = dataService.findDataByParent(superRecordModel.getString("parentname"));
        if(list==null && list.size()<0){
            resultModel.setMem(ErrCode.ERROR_NOCOUNT);
            resultModel.setCode(0);
            return resultModel;
        }
        resultModel.setData(JSONArray.toJSONString(list));
        return  resultModel;
    }

    /**
     * 功能 获取字典组数据
     * liuwb 2016-08-02 13:47
     * 参数 codetype 字典组类型
     * @return
     */
    @RequestMapping("/api/findDataByCodetype")
    @ResponseBody
    public ResultModel findListByCodeType(){
        ResultModel resultModel=new ResultModel();
        SuperRecordModel superRecordModel=this.getSRModel();
        if(StringUtils.isEmpty(superRecordModel.getString("codetype"))){
            resultModel.setMem(ErrCode.ERROR_PARAMETER);
            resultModel.setCode(0);
            return resultModel;
        }
        List<SuperRecordModel> list = dataService.findListByCodeType(superRecordModel.getString("codetype"));
        if(list==null && list.size()<0){
            resultModel.setMem(ErrCode.ERROR_NOCOUNT);
            resultModel.setCode(0);
            return resultModel;
        }
        resultModel.setData(JSONArray.toJSONString(list));
        return  resultModel;
    }

    /**
     * 添加
     * */
    @RequestMapping("/api/data/add")
    @ResponseBody
    public ResultModel apiDataAdd(){
     try {
         SuperRecordModel srModel=this.getSRModel();
         srModel.put("sid", SequenceUtil.Serial());
         srModel.put("codetype","GoodsType");
         srModel.put("","");
         String dataname = StringUtils.getFirstSpell(srModel.getString("dataname")).toUpperCase();
         srModel.put("datavalue",dataname);
         srModel.put("status","1");
         srModel.put("addtime",new Date());
         dataService.addData(srModel);
         return getSucceed(ErrCode.SUCCEED);
     }catch (Exception e){
         e.printStackTrace();
         return getErr(ErrCode.ERROR_SYSTEM);
     }
    }

    /**
     * 删除
     * */
    @RequestMapping("/api/data/delete")
    @ResponseBody
    public ResultModel apiDataDelete(){
        try {
            SuperRecordModel srModel=this.getSRModel();
            srModel.put("codetype","GoodsType");
            dataService.deleteData(srModel);
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 参数：
     *  codetype
     *  pid
     * */
    @RequestMapping("/api/data/parent")
    @ResponseBody
    public ResultModel getDataList(){
        try {
            SuperRecordModel srModel=this.getSRModel();
            List<SuperRecordModel> list=dataService.findDataParentNode(srModel);
            return getSucceed(list,ErrCode.SUCCEED);
        }catch (Exception e){
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }


    /**
     * 删除
     * */
    @RequestMapping("/api/data/update")
    @ResponseBody
    public ResultModel apiDataUpdate(){
        try {
            SuperRecordModel srModel=this.getSRModel();
            srModel.put("codetype","GoodsType");
            if(!StringUtils.isEmpty(srModel.getString("dataname"))){
                String dataname = StringUtils.getFirstSpell(srModel.getString("dataname")).toUpperCase();
                srModel.put("datavalue",dataname);
            }
            dataService.updateData(srModel);
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 参数：
     * sid
     * */
    @RequestMapping("/api/data/detail")
    @ResponseBody
    public ResultModel getDataDetail(){
        try {
            SuperRecordModel srModel=this.getSRModel();
            SuperRecordModel srm=dataService.getDataDetail(srModel);
            return getSucceed(srm,ErrCode.SUCCEED);
        }catch (Exception e){
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }
}
